package com.ehotting.edsta.circuitDesign.gen.element.script;

import com.ehotting.edsta.circuitDesign.domain.EsCircuitCodeDataTemplate;
import com.ehotting.edsta.circuitDesign.domain.EsRuleScript;
import com.ehotting.edsta.circuitDesign.domain.EsSceneParam;
import com.ehotting.edsta.circuitDesign.gen.ScriptConstants;
import com.ehotting.edsta.circuitDesign.gen.element.enums.ScriptTypeEnum;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class FormulaElementScript extends EqualElementScriptAbstract {

    /***
     * ----------公式-------------
     * 正向计算，主要是为某个元器件，找出符合某个值或者某个范围的值
     * @param esRuleScript
     * @param inputParamList
     * @param outputParamList
     * @return
     */
    public String invoke(EsRuleScript esRuleScript, List<EsCircuitCodeDataTemplate> inputParamList, List<EsSceneParam> outputParamList, String functionName){
        return  super.invoke(esRuleScript, inputParamList,  outputParamList,  functionName);
    }



    public String handleResult(List<EsSceneParam> outputParamList, int index){
        StringBuffer buf = new StringBuffer();
        buf.append( ScriptConstants.TAB + "if(componentList.size()>0){"+ ScriptConstants.LINE);
        buf.append(ScriptConstants.TAB(2) + "def map_"+index+" = ElementDataHelper.first(componentList,null);" + ScriptConstants.LINE);
        buf.append(ScriptConstants.TAB(2) + "if(map_"+index+"!=null){" + ScriptConstants.LINE);
        for (int j=0;j<outputParamList.size();j++){
            EsSceneParam esSceneParam = outputParamList.get(j);
            buf.append(ScriptConstants.TAB(3) + esSceneParam.getCode() + "List.clear();" + ScriptConstants.LINE);
            buf.append(ScriptConstants.TAB(3) + esSceneParam.getCode() + "List.add(" + "map_"+index+".get(\"" + esSceneParam.getCode() + "\")" + ");" + ScriptConstants.LINE);
        }
        buf.append(ScriptConstants.TAB(2)+ "outMapList.add(map_"+index+");"+ ScriptConstants.LINE);
        buf.append(ScriptConstants.TAB(2) + "}" + ScriptConstants.LINE);
        buf.append( ScriptConstants.TAB(2) + "componentList.clear();"+ ScriptConstants.LINE);
        buf.append( ScriptConstants.TAB + "}");
        buf.append(ScriptConstants.LINE);
        return buf.toString();
    }


    public boolean isTrue(ScriptTypeEnum code){
        return ScriptTypeEnum.FORMULA ==  code ;
    }


    @Override
    public String getExample() {
        StringBuffer buf = new StringBuffer();
        buf.append("输入格式参数:");
        buf.append("输入格式:");
        buf.append("r1/r2=200").append(ScriptConstants.LINE);
        buf.append("或者多行，回车隔开:");
        buf.append("r1/r2=200").append(ScriptConstants.LINE);
        buf.append("r2/r3=400").append(ScriptConstants.LINE);
        buf.append("或者多属性，分号隔开:");
        buf.append("r1/r2=200;r1.xx/r2.xx=300").append(ScriptConstants.LINE);
        buf.append("r2/r3=400;r2.yy/r2.xx=400").append(ScriptConstants.LINE);

        return buf.toString();
    }

}
